package net.oschina.aibinxiao.security.aes;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;

/**
 * @Author: AiBinXiao
 * @Description: AES对称加密算法不同的实现
 * @Date: 2017/10/14 8:41
 */

public class MyAES {
    private static String src = "imooc security aes";

    public static void main(String[] args) {
        jdkAES();
        bcAES();
    }

    // AES对称加密算法 JDK实现
    public static void jdkAES() {
        try {
            // 生成KEY
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128); // 初始化KEY的位数
            SecretKey secretKey =  keyGenerator.generateKey();
            byte[] keyBytes = secretKey.getEncoded();

            // KEY转换
            Key key = new SecretKeySpec(keyBytes, "AES");

            // 加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("JDK AES encrypt : " + Base64.encodeBase64String(result));
            // JDK AES encrypt : y6MNY/ReesJLWkk9t6KZ3porwdl3pJVyfCQN+t5gqXo=

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(result);
            System.out.println("JDK AES decrypt :" + new String(result));
            // JDK AES decrypt :imooc security aes
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // AES对称加密算法 Bouncy Castle实现
    public static void bcAES() {
        try {
            Security.addProvider(new BouncyCastleProvider());

            // 生成KEY
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
            keyGenerator.getProvider();
            keyGenerator.init(128); // 位数
            SecretKey secretKey = keyGenerator.generateKey(); // 生成密钥
            byte[] keyBytes = secretKey.getEncoded();

            // KEY转换
            Key key = new SecretKeySpec(keyBytes, "AES");

            // 加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 加密算法/工作模式/填充方式
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("Bouncy Castle AES encrypt : " + Hex.encodeHexString(result));
            // Bouncy Castle AES encrypt : d070d1e16973fd2a680bf0518ba7e96d740325b8c7cfe6caf9c0d26599d5a267

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(result);
            System.out.println("Bouncy Castle AES decrypt : " + new String(result));
            // Bouncy Castle AES decrypt : imooc security aes
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
